package com.qf.service.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.bean.R;
import com.qf.common.dto.AreaDto;
import com.qf.common.model.QfArea;
import com.qf.service.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 行政区管理
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/service/area/")
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 查询所有
     * @param dto
     * @return
     */
    @PostMapping("/all")
    public R<List> findAll(@RequestBody(required = false)AreaDto dto) {
        List<QfArea> list = areaService.all(dto);
        return R.ok(list);
    }

    /**
     * 根据id查询
     * @return
     */
    @GetMapping("/id/{id}")
    public R<QfArea> selectById(@PathVariable(value = "id") String id) {
        QfArea one = areaService.selectById(id);
        return R.ok(one);
    }

    /**
     * 分页查询
     * @return
     */
    @PostMapping("/list")
    public R<Page<QfArea>> findByPage(@RequestBody(required = false) AreaDto dto) {
        Page<QfArea> page = areaService.list(dto);
        return R.ok(page);
    }

    /**
     * 添加
     * @param entity
     * @return
     */
    @PostMapping("/add")
    public R<QfArea> add(@RequestBody QfArea entity) {
        QfArea add = areaService.add(entity);
        return R.ok(add);
    }

    /**
     * 修改
     * @param entity
     * @return
     */
    @PostMapping("/update")
    public R<QfArea> update(@RequestBody QfArea entity) {
        QfArea update = areaService.update(entity);
        return R.ok(update);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R<Boolean> delete(@NotNull @PathVariable(value = "id") String id) {
        boolean delete = areaService.delete(id);
        return R.ok(delete);
    }
}
